package XML;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class XML {

	/**
	 * @param args
	 * @throws DocumentException 
	 */
	public static void main(String[] args) throws DocumentException {
		File file = new File("d:"+File.separator+"config.xml");
		SAXReader saxReader = new SAXReader(); 
		saxReader.setEncoding("utf-8");
		try {
			Document d = saxReader.read(file);
			Element e = d.getRootElement();
			Iterator iterator = e.elementIterator();
			while(iterator.hasNext()){
				Element el = (Element) iterator.next();
				System.out.println(el.elementText("name"));
				Iterator i = el.elementIterator();
				while(i.hasNext()){
					Element ele = (Element) i.next();
					if(ele.getName().equals("name")){
						ele.setText("随便加");
					}
					}
			}
			//xml写
			XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(file),"utf-8"));
			writer.write(d);
			writer.close();
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		
//		SAXReader reader = new SAXReader();
//		Document document = reader.read(new File("d:\\config.xml"));
//		Element e = document.getRootElement();
//		List list = e.elements();
//		System.out.println(list.size());
	}

}
